// pages/payment/payment.js
var that,
  api = new (require('../../utils/api.js')),
  app = getApp(),
  util = new (require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this;
    that.getdata()
  },
  getdata(){
    let sitedata={}
    let { msgTplIdArr } = app.logininfo
    let page=getCurrentPages(),
    data=page[page.length-2].data;
    data.sitearr.forEach(x=>{
      if(x.id==data.siteid){
        sitedata=x
      }
    })
    // console.log(data,'datadata');
    let rechargedate=data.list[data.selindex]
    
    // data.newprice=parseInt(data.newprice)
    that.setData({sitedata,rechargedate,msgTplIdArr})
  },
  pay(){
    let{msgTplIdArr} = that.data,newmsgTplIdArr=[]
    newmsgTplIdArr.push(msgTplIdArr.accountChange,msgTplIdArr.couponOverdue,msgTplIdArr.scoreChange)
    wx.requestSubscribeMessage({
      tmplIds:newmsgTplIdArr,
      success: function (res) {
        console.log(res,'resresresresres');
        wx.showLoading({
          title: '加载中',
        })
        let {sitedata,rechargedate}=that.data
        api.chargeadd({chargeId:rechargedate.chargeId,stationId:sitedata.id},res1=>{
          if(res1.data.code==0){
            api.chargewxstartpay({orderId:res1.data.entity.data.orderId},res=>{
              wx.hideLoading()
              if(res.succ==1){
                wx.redirectTo({
                  url: '../paysucc/paysucc?data='+JSON.stringify(rechargedate)+'&orderId='+res1.data.entity.data.orderId,
                })
              }else{
                wx.showModal({
                  content:res.msg,
                  showCancel:!1,
                })
              }
            })
          }else if(res1.data.code==20637){
            wx.hideLoading()
             const pages = getCurrentPages()
            const prevPage = pages[pages.length-2] // 上一页// 调用上一个页面的setData 方法，将数据存储
            prevPage.setData({
              code:20637,
            },()=>{
              wx.navigateBack({
                 delta: 1
              })
            })
          }else{
            wx.hideLoading()
            wx.showModal({
              content:res.msg,
              showCancel:!1,
            })
          }
        })
      },
      fail: function (err) {
        console.log(err,'errrrrr');
        wx.showLoading({
          title: '加载中',
        })
        let {sitedata,rechargedate}=that.data
        console.log(sitedata,'sitedatasitedata');
        console.log(rechargedate,'rechargedate');
        api.chargeadd({chargeId:rechargedate.chargeId,stationId:sitedata.id},res1=>{
          if(res1.data.code==0){
            api.chargewxstartpay({orderId:res1.data.entity.data.orderId},res=>{
              wx.hideLoading()
              console.log(res,'res');
              if(res.succ==1){
                wx.redirectTo({
                  url: '../paysucc/paysucc?data='+JSON.stringify(rechargedate)+'&orderId='+res1.data.entity.data.orderId,
                })
              }else{
                wx.showModal({
                  content:res.msg,
                  showCancel:!1,
                })
              }
            })
          }else if(res1.data.code==20637){
            wx.hideLoading()
             const pages = getCurrentPages()
            const prevPage = pages[pages.length-2] // 上一页// 调用上一个页面的setData 方法，将数据存储
            prevPage.setData({
              code:20637,
            },()=>{
              wx.navigateBack({
                 delta: 1
              })
            })
          }else{
            wx.hideLoading()
            wx.showModal({
              content:res.msg,
              showCancel:!1,
            })
          }
        })
      }
    })
  
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})